﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.IO;

namespace jQuery.WebControls
{
    abstract class Estilo
    {              
        private Page pagina = null;

        public Estilo(Page p)
        {
            pagina = p;
            if (pagina == null)
                throw new Exception( jQuery.Messages.Estilo_err1);
        }
        protected abstract string NombreCss { get; }
        /// <summary>
        /// Relative path to the stylesheet page associated with the selected preset style
        /// </summary>
        protected abstract string RecursoUrlCss { get; }
        /// <summary>
        /// Stylesheet url
        /// </summary>
        protected string UrlArchivoCss { get { return RecursoUrlCss + "\\" + NombreCss; } }
        /// <summary>
        /// Web page where stylesheet will be added
        /// </summary>
        protected Page Pagina { get { return pagina; } }
        /// <summary>
        /// Checks if the stylesheet already exists in the website and if it was included in current page header. If it doesn't, 
        /// exists in CSS folder, all the mandatory files are added and if it doesn't exists in current web page, the 
        /// link tag is added referencing to the stylesheet
        /// </summary> 
        protected abstract void ComprobarCSS();
        /// <summary>
        /// Return the control to link the resource source with the HTML page
        /// </summary>
        /// <param name="src">Relative path of the stylesheet</param>
        /// <returns>Return a HhtmlGenericControl with the tag link</returns>       
        protected Control getCssHeader(string src)
        {
            HtmlGenericControl link = new HtmlGenericControl("link");
            link.Attributes.Add("rel", "stylesheet");
            link.Attributes.Add("rev", "stylesheet");
            link.Attributes.Add("href", src);            
            return link;
        }
        /// <summary>
        /// Checks if there is a link tag on header page with the specific relative path
        /// </summary>
        /// <param name="src">Stylesheet url</param>
        /// <returns>Return true if exists</returns>
        protected bool CheckCssHeader(string src)
        {
            if (Pagina.Header != null)
            {
                for (int i = 0; i < Pagina.Header.Controls.Count; i++)
                {
                    if (Pagina.Header.Controls[i].GetType() == typeof(HtmlGenericControl))
                    {
                        if (((HtmlGenericControl)Pagina.Header.Controls[i]).TagName == "link" &&
                            ((HtmlGenericControl)Pagina.Header.Controls[i]).Attributes["href"] == src)
                        {
                            return true;
                        }
                    }
                }
                return false;
            }
            else
            {
                throw new Exception(Messages.E_err1);
            }            
        }
        /// <summary>
        /// Create the folder CSS on website where the controls are used if it doesn't exist
        /// </summary>
        /// <param name="folderPath">Folder where the all the style resources will be stored</param>
        protected void CrearCssDir(string folderPath)
        {
            if (!Directory.Exists(folderPath))
            {
                Directory.CreateDirectory(folderPath);
            }
        }
        /// <summary>
        /// Checks if the default CSS is in header page. If it's not, the link is added.
        /// Comprueba que el estilo predefinido CssPredefinido está en la cabecera. Si no está añade la etiqueta link
        /// correspondiente
        /// </summary>
        public void ComprobarCSSenPagina()
        {
            ComprobarCSS();
        }
        /// <summary>
        /// Remove the link where the src attribute is equal to UrlArchivoCss property
        /// </summary>
        public void QuitarCSSenPagina()
        {
            if (CheckCssHeader(Pagina.ResolveClientUrl(UrlArchivoCss)))
            {
                Pagina.Header.Controls.Remove(getCssHeader(Pagina.ResolveClientUrl(UrlArchivoCss)));
            }            
        }
    }
}
